package com.springboot.util.common;

/**
 * 方法描述 树结构公共方法
 * @since: 1.0
 * @author: zyj
 * @date: 2022/3/2 11:48
 */


import java.util.ArrayList;
import java.util.List;

import com.springboot.returndata.vo.CommonTreeVO;
import org.apache.commons.lang.StringUtils;

/**
 * @author zyj
 * @description
 * @date 2018/7/31 14:11
 **/
public class TreeVoUtil {

    public static String key_child = "child";
    public static String key_children = "children";
    public static String key_replyBody = "replyBody";
    public static <T> CommonTreeVO<T> getTreeVo(List<CommonTreeVO<T>> listTreeVo, String parentId){
        CommonTreeVO treeVo = null;
        if(null==parentId||parentId.isEmpty()){
            return treeVo;
        }

        int length = listTreeVo.size()-1;
        for(int i=length;i>=0;i--){
            if(listTreeVo.size()<=i){
                break;
            }
            CommonTreeVO menuVoNext = listTreeVo.get(i);
            if(parentId.equals(menuVoNext.getId())){
                treeVo = menuVoNext;
                putValueVo(listTreeVo, menuVoNext, parentId);
            }
        }

        return treeVo;
    }


    public static <T> List<CommonTreeVO> getListTreeVo(List<CommonTreeVO<T>> listTreeVo, String parentId){
        List<CommonTreeVO> treeVos = new ArrayList<>();
        if(null==parentId||parentId.isEmpty()){
            return treeVos;
        }

        for(int i=listTreeVo.size()-1;i>=0;i--){
            System.out.println(i);
            if(listTreeVo.size()<=i){
                break;
            }
            CommonTreeVO treeVoNext = listTreeVo.get(i);
            if(parentId.equals(treeVoNext.getParentId())){
                putValueVo(listTreeVo, treeVoNext, treeVoNext.getId());
                treeVos.add(treeVoNext);
            }
        }
        return treeVos;
    }



    public static <T> void  putValueVo(List<CommonTreeVO<T>> listTreeVo, CommonTreeVO treeVo, String parentId){
        List<CommonTreeVO> listTree = new ArrayList<>();
        for(int i=listTreeVo.size()-1;i>=0;i--){
            CommonTreeVO menuVo1 = listTreeVo.get(i);
            if(menuVo1.getParentId()!=null&&menuVo1.getParentId().equals(parentId)){
                listTree.add(menuVo1);
                putValueVo(listTreeVo, menuVo1, menuVo1.getId());
            }
        }
        treeVo.setChild(listTree);
    }

    /**
     * 转为树列表
     * @param listTreeVo 原始值
     * @param parentId 父节点
     * @param key 字节点名称 默认children
     * @return
     */
    public static List getListTreeVoCommon(List<? extends CommonTreeVO> listTreeVo, String parentId, String key){
        List<CommonTreeVO> treeVos = new ArrayList<>();
        if(null==parentId||parentId.isEmpty()){
            return treeVos;
        }

        for(int i=listTreeVo.size()-1;i>=0;i--){
            System.out.println(i);
            if(listTreeVo.size()<=i){
                break;
            }
            CommonTreeVO treeVoNext = listTreeVo.get(i);
            if(parentId.equals(treeVoNext.getParentId())){
                putValueVoCommon(listTreeVo, treeVoNext, treeVoNext.getId(), key);
                treeVos.add(treeVoNext);
            }
        }
        return treeVos;
    }

    public static void  putValueVoCommon(List<? extends CommonTreeVO> listTreeVo, CommonTreeVO treeVo, String parentId, String key){
        List<CommonTreeVO> listTree = new ArrayList<>();
        for(int i=listTreeVo.size()-1;i>=0;i--){
            CommonTreeVO menuVo1 = listTreeVo.get(i);
            if(menuVo1.getParentId()!=null&&menuVo1.getParentId().equals(parentId)){
                listTree.add(menuVo1);
                putValueVoCommon(listTreeVo, menuVo1, menuVo1.getId(), key);
            }
        }
        if(StringUtils.isNotEmpty(key) && key.equals(key_children)){
            treeVo.setChildren(listTree);
        }if(StringUtils.isNotEmpty(key) && key.equals(key_replyBody)){
            treeVo.setReplyBody(listTree);
        }else {
            treeVo.setChild(listTree);
        }
    }

}

